package ferran.tomas.ta;

import java.util.ArrayList;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class NewProduct extends Activity {
	EditText id, price, ratings, amount;
	private ServidorPrincipal sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.newproduct);
		
		id = (EditText) findViewById(R.id.editID);
		price = (EditText) findViewById(R.id.editPrice);
		ratings = (EditText) findViewById(R.id.editRating);
		amount = (EditText) findViewById(R.id.editAmount);
		sp = ServidorPrincipal.getInstancia();

		
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		menu.setGroupVisible(R.id.grupo1, false);
		menu.setGroupVisible(R.id.grupo3, false);
		menu.setGroupVisible(R.id.grupo4, false);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
			
		case R.id.back:
			back();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	public void back() {
	Log.d("Back", "Esta es la funcion de back");
	finish();
		
	}
	
	public void add_product(View v) {
		
		String ID = id.getText().toString();
		String precio = price.getText().toString();
		String rating = ratings.getText().toString();
		String cantidad = amount.getText().toString();

		if (sp.insertarProducto(ID, precio, rating, cantidad)) {
			back();

		} else {
			// Mostrar mensaje de error
			String error = sp.getRespuestaServidorPrincipal();
			Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT)
					.show();
		}
	}
}
